package com.ch01.ex23;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadFactory;

/**
 * Created by zc on 2016/1/27.
 */
public class MyThreadFactory implements ThreadFactory {
    private int counter;
    private String name;
    private List<String> stats;

    public MyThreadFactory(String name) {
        this.name = name;
        this.counter = 0;
        this.stats = new ArrayList<>();
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r, name + "-Thread_" + counter);
        counter++;
        stats.add(String.format("Created thread %d with name %s on %s\n",
                thread.getId(), thread.getName(),new Date()));
        return thread;
    }

    public String getStats(){
        StringBuffer sb = new StringBuffer();

        for(String key : stats){
            sb.append(key);
        }
        return sb.toString();
    }
}
